// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/vector/variant_to_vector.h>
#include <pollux/vector/flat_vector.h>

namespace kumo::pollux::core {
    namespace {
        template<TypeKind KIND>
        ArrayVectorPtr variantArrayToVectorImpl(
            const TypePtr &arrayType,
            const std::vector<variant> &variantArray,
            pollux::memory::MemoryPool *pool) {
            using T = typename TypeTraits<KIND>::NativeType;

            // First generate internal array_vector elements.
            const size_t variantArraySize = variantArray.size();

            // Create array elements flat vector.
            auto arrayElements = BaseVector::create<FlatVector<T> >(
                arrayType->childAt(0), variantArraySize, pool);

            // Populate internal array elements (flat vector).
            for (vector_size_t i = 0; i < variantArraySize; i++) {
                const auto &value = variantArray[i];
                if (!value.isNull()) {
                    // `getOwnedValue` copies the content to its internal buffers (in case of
                    // string/StringView); no-op for other primitive types.
                    arrayElements->set(i, T(value.value<KIND>()));
                } else {
                    arrayElements->set_null(i, true);
                }
            }

            // Create ArrayVector around the FlatVector containing array elements.
            BufferPtr offsets = allocateOffsets(1, pool);
            BufferPtr sizes = allocateSizes(1, pool);

            auto rawSizes = sizes->asMutable<vector_size_t>();
            rawSizes[0] = variantArraySize;

            return std::make_shared<ArrayVector>(
                pool, arrayType, nullptr, 1, offsets, sizes, arrayElements);
        }
    } // namespace

    ArrayVectorPtr variantArrayToVector(
        const TypePtr &arrayType,
        const std::vector<variant> &variantArray,
        pollux::memory::MemoryPool *pool) {
        POLLUX_CHECK_EQ(TypeKind::ARRAY, arrayType->kind());

        if (arrayType->childAt(0)->is_unKnown()) {
            return variantArrayToVectorImpl<TypeKind::UNKNOWN>(
                arrayType, variantArray, pool);
        }

        return POLLUX_DYNAMIC_SCALAR_TYPE_DISPATCH(
                    variantArrayToVectorImpl,
                    arrayType->childAt(0)->kind(),
                    arrayType,
                    variantArray,
                    pool);
    }
} // namespace kumo::pollux::core
